<!DOCTYPE html>
<html lang="zh-CN">
  <head>
    <meta charset="utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"
    />
    <meta name="author" content="林镭特" />
    <title></title>
    <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
    <style>
      .box {
        width: 75px;
        height: 35px;
        border-radius: 50px;
        background-color: red;
        position: relative;
      }

      .ball {
        position: absolute;
        border-radius: 50%;
        height: 30px;
        width: 30px;
        background-color: white;
        margin: 2.5px 2px;
        transition: all 0.2s;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <my-open></my-open>
    </div>

    <script id="my-open" type="text/html">
      <div>
          <div class="box" @click="click" :style="bgc" >
              <div class="ball" :style="ball" ></div>
          </div>
      </div>
    </script>

    <script>
      Vue.component("my-open", {
        template: "#my-open",
        data() {
          return {
            value: false,
            bgc1: "#13ce66",
            bgc2: "#ff4949",
          };
        },
        methods: {
          click() {
            this.value = !this.value;
          },
        },
        computed: {
          ball() {
            if (this.value == true) {
              return {
                left: "40px",
              };
            } else {
              return {
                left: "0px",
              };
            }
          },
          bgc() {
            if (this.value == true) {
              return {
                background: this.bgc1,
              };
            } else {
              return {
                background: this.bgc2,
              };
            }
          },
        },
      });

      let app = new Vue({
        el: "#app",
        data: {},
      });
    </script>
  </body>
</html>
